import codecs
import csv

import numpy as np
import pandas as pd
from matplotlib.ticker import MultipleLocator
import matplotlib.pylab as plt


# 曲线实例
# plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
# plt.ylabel('some numbers')
# plt.show()

# 柱状图实例
# names = ['group_a', 'group_b', 'group_c']
# values = [1, 10, 100]
# plt.bar(names, values)
# plt.show()

def draw_bar(path, x_label, y_label, volume):
    df = pd.read_excel(path)
    group = df.groupby(x_label, as_index=False)[y_label].sum()
    c_df = pd.DataFrame(group).sort_values(by=y_label, ascending=False)
    # print(c_df)

    # 坐标轴刻度设置
    ax = plt.gca()
    # 把x轴的刻度间隔设置为1，并存在变量里
    # x_major_locator = MultipleLocator(1)
    # ax.xaxis.set_major_locator(x_major_locator)

    # 把y轴的刻度间隔设置为 "百万"，并存在变量里
    y_major_locator = MultipleLocator(volume)
    ax.yaxis.set_major_locator(y_major_locator)

    # 设置坐标文字
    plt.xlabel("类型", fontsize=14)
    plt.ylabel("成交额度(" + str(volume) + ")", fontsize=14)

    # 设置坐标文字方向
    plt.xticks(rotation=90)

    # 设置图中文字样式
    plt.rcParams['font.sans-serif'] = ['KaiTi', 'SimHei', 'FangSong']  # 汉字字体,优先使用楷体，如果找不到楷体，则使用黑体
    plt.rcParams['font.size'] = 9  # 字体大小
    plt.rcParams['axes.unicode_minus'] = False  # 正常显示负号

    # 设置数据
    plt.bar(c_df[x_label], c_df[y_label])
    plt.show()


path = 'C:\\xiaxinyu\\Payment_2018.xlsx'
x_label = 'TYPE_CD'
y_label = 'PAY_AMT'
draw_bar(path, x_label, y_label, 10000000)
